package com.todo.strategy.online.application.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.todo.component.redis.RedisService;
import com.todo.strategy.common.constant.StrategyOnlineKeyBuild;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyBO;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyCountBO;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyDetailBO;
import com.todo.strategy.online.application.entity.bo.PlatformStrategyExtendBO;
import com.todo.strategy.online.application.service.PlatformStrategyService;
import com.todo.strategy.online.infrastructure.entity.po.PlatformStrategyDetailPO;
import com.todo.strategy.online.infrastructure.entity.po.PlatformStrategyPO;
import com.todo.strategy.online.infrastructure.helper.es.entity.BasicStrategyEntity;
import com.todo.strategy.online.infrastructure.helper.es.mapper.BasicStrategyEntityMapper;
import com.todo.strategy.online.infrastructure.repository.PlatformStrategyRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (PlatformStrategy)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:47
 */
@Slf4j
@Service("platformStrategyService")
public class PlatformStrategyServiceImpl implements PlatformStrategyService {

    @Resource
    private PlatformStrategyRepository platformStrategyRepository;
    @Resource
    private RedisService redisService;
    @Resource
    private BasicStrategyEntityMapper basicStrategyEntityMapper;


    @Override
    public PlatformStrategyBO queryPlatformStrategyInfoById(Long strategyId) {
        if (strategyId == null || strategyId < 0) {
            return null;
        }
        String redisKey = StrategyOnlineKeyBuild.strategyPlatformDetailKey(strategyId);
        PlatformStrategyBO alreadyData = JSONObject.parseObject(redisService.get(redisKey), PlatformStrategyBO.class);
        if (alreadyData != null) {
            return alreadyData;
        }
        PlatformStrategyPO platformStrategyPO = platformStrategyRepository.queryBasePlatformStrategyById(strategyId);
        PlatformStrategyBO platformStrategyBO = StrategyConvert.convertToPlatformStrategyBO(platformStrategyPO);
        if (platformStrategyBO == null) {
            return null;
        }
        redisService.set8H(redisKey, JSONObject.toJSONString(platformStrategyBO));
        log.info("PlatformStrategyService#queryPlatformStrategyInfoById 数据库根据id查询平台攻略信息完成，id:{},结果:{}", strategyId, JSONObject.toJSONString(platformStrategyBO));
        return platformStrategyBO;
    }

    @Override
    public PlatformStrategyExtendBO queryPlatformStrategyExtendById(Long strategyId) {
        if (strategyId == null || strategyId < 0) {
            return null;
        }
        String redisKey = StrategyOnlineKeyBuild.strategyPlatformExtendKey(strategyId);
        PlatformStrategyExtendBO alreadyData = JSONObject.parseObject(redisService.get(redisKey), PlatformStrategyExtendBO.class);
        if (alreadyData != null) {
            return alreadyData;
        }
        PlatformStrategyDetailPO platformStrategyDetailPO = platformStrategyRepository.queryPlatformStrategyDetailById(strategyId);
        PlatformStrategyExtendBO platformStrategyExtendBO = StrategyConvert.convertToPlatformStrategyExtendBO(platformStrategyDetailPO);
        if (platformStrategyExtendBO == null) {
            return null;
        }
        redisService.set8H(redisKey, JSONObject.toJSONString(platformStrategyExtendBO));
        log.info("PlatformStrategyService#queryPlatformStrategyExtendById 数据库根据id查询平台攻略扩展信息完成，id:{},结果:{}", strategyId, JSONObject.toJSONString(platformStrategyExtendBO));
        return platformStrategyExtendBO;
    }

    @Override
    public PlatformStrategyDetailBO queryPlatformStrategyDetailById(Long strategyId) {
        if (strategyId == null || strategyId < 0) {
            return null;
        }
        PlatformStrategyBO platformStrategyBO = queryPlatformStrategyInfoById(strategyId);
        if (platformStrategyBO == null) {
            return null;
        }
        PlatformStrategyExtendBO platformStrategyExtendBO = queryPlatformStrategyExtendById(strategyId);
        PlatformStrategyDetailBO platformStrategyDetailBO = new PlatformStrategyDetailBO();
        platformStrategyDetailBO.setPlatformStrategyBO(platformStrategyBO);
        platformStrategyDetailBO.setPlatformStrategyExtendBO(platformStrategyExtendBO);

        return platformStrategyDetailBO;
    }

    @Override
    public List<PlatformStrategyBO> queryPlatformStrategyInfoByIdList(List<Long> strategyIdList) {
        if (CollectionUtils.isEmpty(strategyIdList)) {
            return Lists.newArrayList();
        }
        List<PlatformStrategyBO> result = new ArrayList<>();
        List<Long> missedData = new ArrayList<>();
        for (Long strategyId : strategyIdList) {
            String key = StrategyOnlineKeyBuild.strategyPlatformDetailKey(strategyId);
            PlatformStrategyBO alreadyData = JSONObject.parseObject(redisService.get(key), PlatformStrategyBO.class);
            if (alreadyData != null) {
                result.add(alreadyData);
            } else {
                missedData.add(strategyId);
            }
        }

        List<PlatformStrategyPO> platformStrategyPOList = platformStrategyRepository.queryPlatformStrategyByIdList(missedData);
        List<PlatformStrategyBO> collect = platformStrategyPOList.stream()
                .map(StrategyConvert::convertToPlatformStrategyBO)
                .collect(Collectors.toList());
        result.addAll(collect);
        collect.forEach(item -> {
            redisService.setOneDay(StrategyOnlineKeyBuild.strategyPlatformDetailKey(item.getStrategyId()), JSONObject.toJSONString(item));
        });
        return result;
    }

    @Override
    public Boolean updatePlatformStrategyCount(PlatformStrategyCountBO countBO) {
        if (countBO == null) {
            return false;
        }
        PlatformStrategyBO platformStrategyBO = queryPlatformStrategyInfoById(countBO.getPlatformId());
        if (platformStrategyBO == null) {
            return false;
        }
        PlatformStrategyPO platformStrategyPO = new PlatformStrategyPO();
        platformStrategyPO.setId(countBO.getPlatformId());
        platformStrategyPO.setLikeCount(countBO.getLikeCount());
        platformStrategyPO.setViewCount(countBO.getSeeCount());
        platformStrategyPO.setCommentCount(countBO.getCommentCount());
        platformStrategyPO.setCollectCount(countBO.getCollectCount());
        Boolean updated = platformStrategyRepository.updatePlatformStrategyInfo(platformStrategyPO);
        if (updated) {
            redisService.del(StrategyOnlineKeyBuild.strategyPlatformDetailKey(countBO.getPlatformId()));
            BasicStrategyEntity strategyEntity = new BasicStrategyEntity();
            LambdaEsQueryWrapper<BasicStrategyEntity> wrapper = new LambdaEsQueryWrapper<>();
            wrapper.eq(BasicStrategyEntity::getMysqlId, platformStrategyPO.getId());
            wrapper.set(BasicStrategyEntity::getLikeCount, countBO.getLikeCount());
            wrapper.set(BasicStrategyEntity::getViewCount, countBO.getSeeCount());
            wrapper.set(BasicStrategyEntity::getCommentCount, countBO.getCommentCount());
            wrapper.set(BasicStrategyEntity::getCollectCount, countBO.getCollectCount());
            basicStrategyEntityMapper.update(strategyEntity, wrapper);
        }
        return true;
    }

}
